<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends CI_Model {
    public function __construct() {
    	parent::__construct();
    }
	
    public function getPages($options = array()) {
    	$options = _default(array('sortDirection' => 'asc'), $options);
    	$queryFields = array('pag_id', 'pag_description', 'pag_name', 'pag_title', 'pag_homepage');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit]'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit]']);
    	
    	if (isset($options['sortBy']))
    		$this->db->order_by($options['sortBy'], $options['sortDirection']);
    	$query = $this->db->get('acs_pages');
    	if ($query->num_rows() == 0)
    		return FALSE;
    	if (isset($options['pag_name']) || isset($options['pag_id']) || isset($options['pag_homepage']))
    		return $query->row_array(0);
    	else
    		return $query->result_array();
    }
    
    public function updatePage($options = array()) {
		$fields = array('pag_title', 'pag_name', 'pag_sso_name', 'pag_description', 'pag_hompage','pag_author', 'pag_date', 'pag_content');
		$options = _default(array('pag_date' => date('Y-m-d H:i:s')), $options);
		if (!_required(array('pag_id'), $options)) {
			return FALSE;
		}
		foreach ($fields as $field) {
			if (isset($options[$field]))
				$this->db->set($field, $options[$field]);
		}
		$this->db->where('pag_id', $options['pag_id']); 
		$this->db->update('acs_pages');
		return $this->db->affected_rows();
    }
    
    public function insertPage($options = array()) {
		$fields = array('pag_title', 'pag_name', 'pag_sso_name', 'pag_description', 'pag_homepage','pag_author', 'pag_date', 'pag_content');
		$options = _default(array('pag_date' => date('Y-m-d H:i:s'), 'pag_homepage' => 'N'), $options);
		if (!_required(array('pag_title', 'pag_name'), $options)) {
			return FALSE;
		}
		foreach ($fields as $field) {
			if (isset($options[$field]))
				$this->db->set($field, $options[$field]);
		}
		$this->db->insert('acs_pages');
		return $this->db->insert_id();
    }
    
	public function deletePage($options = array()) {
    }
}
